/**************************************************************************//**
* @file     CANOpenSlave.h
* @brief    Head file of CANOpenSlave.c
* @author   Shawn Huang
* @date
* @version
*******************************************************************************
* Copyright (c) DeltaTec 2016
* All right reserved.
******************************************************************************/

#ifndef CAN_COMM_H
#define CAN_COMM_H

/******************************************************************************
* INCLUDE FILE
******************************************************************************/
// Include system file

// Include project file
#include "common.h"

/******************************************************************************
* DEFINE DESCRIPTION
******************************************************************************/
#define BAURDRATE             250    //250kbps

#define BROADCAST_ID       0
#define GPS_NODE_ID        2

#define DATA_FRAME         0x0000
#define REMOTE_FRAME       0x4000

#define GPS_CODE_MASK      0x024A
#define GPS_TPDO1_CODE     0x024A
#define GPS_TPDO2_CODE     0x034A
#define GPS_TPDO3_CODE     0x044A
#define GPS_TPDO4_CODE     0x054A

#define NODE_CONTROL_CODE  0x0000
#define NODE_GUARD_CODE    0x0700
#define SYNC_CODE          0x0080
#define EMERGENCY_CODE     0x0080
#define RPDO_1_CODE        0x0200
#define RPDO_2_CODE        0x0300
#define RPDO_3_CODE        0x0400
#define RPDO_4_CODE        0x0500
#define TPDO_1_CODE        0x0180
#define TPDO_2_CODE        0x0280
#define TPDO_3_CODE        0x0380
#define TPDO_4_CODE        0x0480

#define LOST_TIME_500MS    5   //100ms*5=500ms
#define LOST_TIME_1000MS   10  //100ms*10=1000ms
#define TX_TIMER_10MS      1   //10ms*1=10ms
#define TX_TIMER_50MS      5   //10ms*5=50ms
#define MESSAGE_0_LOST_TIME  7000 //10ms*100=1000ms
#define TX_TIMER_10S       1000 //10ms*1000=10s

#define DELAY_500MS   50    //50ms*10=500ms
#define DELAY_5S   250    //20ms*250=5s
#define DELAY_1MIN  3000   //20ms*3000=1min
#define DELAY_10MIN 30000  //20ms*30000=10min

#define GPS_ENABLE_FLAG  lift_setup.bSpare1 //lift_setup.bChassisLiftUpPwm
/******************************************************************************
* ENUM DESCRIPTION
******************************************************************************/
enum EN_CAN_RX_BUFFER
{
   // define the index of rx buffer
   RX_TPDO1_GPS,
   RX_TPDO_GPSLIB,
};

/******************************************************************************
* STRUCTURE DESCRIPTION
******************************************************************************/
struct ST_MC_DATA
{
   UINT16 u16McSpeed1;         ///<real Speed of left MC
   UINT16 u16McSpeed2;         ///<real Speed of right MC
   UINT16 u16McSpeed3;         ///<real Speed of pump MC
   UINT16 u16McCurrent1;         ///<real current of left MC
   UINT16 u16McCurrent2;         ///<real current of right MC
   UINT16 u16McCurrent3;         ///<real current of pump MC
   UINT8  u8BattCap;             ///<battery capacity
   UINT8  u8McState;            ///< The MC state
   UINT8  u8ContactOpenReadyFlag; ///
   UINT8  u8EnableReadyFlag; ///

   UINT8  u8MCErrorCodeLeft;        ///<Error Code from MC left
   UINT8  u8MCErrorCodeRight;       ///<Error Code from MC right
   UINT8  u8MCErrorCodePump;        ///<Error Code from MC pump

   UINT8  u8MCError1;        		///<Error1 Code from MC
   UINT8  u8MCError2;               ///<Error2 Code from MC
   UINT8  u8MCError3;               ///<Error3 Code from MC
   UINT8  u8MCInitialError;         ///<Initial error Code from MC
   UINT8  u8MCPrechargeError;       ///<Precharge error Code from MC
   UINT8  u8MCWarning1;        		///<Warning1 Code from MC
   UINT8  u8MCWarning2;             ///<Warning2 Code from MC
};

/******************************************************************************
* EXTERN CONSTANT DESCRIPTION
******************************************************************************/

/******************************************************************************
* EXTERN VARIABLES DESCRIPTION
******************************************************************************/
extern struct ST_MC_DATA stMcData;
/******************************************************************************
* EXTERN FUNCTION PROTOTYPE
******************************************************************************/


#define CanSendMsgMaxLen	(10)
#define TX_FIFO_PUSH_EN	(u8GetCanMasterDriveRxFifoTotalLen(&stCanTxFifo)<CanSendMsgMaxLen)
typedef struct
{
	UINT16 u16_id_hi;
	UINT16 u16_id_Lo;
	UINT8 u8Length;
	UINT8 u8_buffer[8];
}structCanTxMsg;
typedef struct
{
	structCanTxMsg CanTxMsg[CanSendMsgMaxLen];
	UINT8 u8Head;
	UINT8 u8Rear;
	UINT8 u8TotalLen;
}structCanTxFifo;
extern structCanTxFifo stCanTxFifo;
UINT8 u8GetCanMasterDriveRxFifoTotalLen(structCanTxFifo *pFifo);
void vClearCanTxFifo(structCanTxFifo *pFifo);
void vPushCanTxFifo(structCanTxFifo *pFifo,UINT16 id_hi, UINT16 id_lo, UINT8 length, UINT8 * tx_buf);
void vPopCanTxFifo(structCanTxFifo *pFifo,structCanTxMsg *pCanRxMsg);
void vCanTxFifoProcess(void);//1ms
void vCanFastRxProcess(void);//200us



void vCanInitial(void);
void vCanRxProcess(void);
void vCanTxProcess(void);
UINT16 u16GetCommLostTime(void);
UINT16 u16GetMCCommLostState(void);

#endif
/******************************************************************************
* End of module
******************************************************************************/
